<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Logic\Menu as MenuLogic;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\Menu as MenuRequest;

class MenuController extends Controller
{
    private $menuLogic;
    private $menuRequest;

    public function __construct()
    {
        $this->menuLogic = new MenuLogic();
        $this->menuRequest = new MenuRequest();
    }

    public function index()
    {
        return view('menu.index');
    }

    public function list()
    {
        $result = [
            'code' => 0,
            'msg' => '获取数据成功',
            'count' => $this->menuLogic->count(),
            'data' => $this->menuLogic->list(),
        ];
        return response()->json($result);
    }

    public function add(Request $request, $id = 0)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'新增失败！'];
            $data['pid'] = $request->post('pid', 0);
            $data['menu_type'] = $request->post('menu_type',1);
            $data['menu_name']= $request->post('menu_name','');
            $data['menu_url']= $request->post('menu_url','');
            $data['menu_sort'] = $request->post('menu_sort',0);
            $data['menu_icon']= $request->post('menu_icon','');
            $data['menu_status'] = $request->post('menu_status',1);

            $validator = Validator::make($request->all(), $this->menuRequest->rules(), $this->menuRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if(!$this->menuLogic->checkName($data['menu_name'],(int)$data['pid'])){
                $result['msg'] = '菜单名称已经存在！';
                return response()->json($result);
            }

            if ($data['menu_url'] != '#' && !empty($data['menu_url'])){
                if(!$this->menuLogic->checkUrl($data['menu_url'],(int)$id)){
                    $result['msg'] = '链接地址已经存在！';
                    return response()->json($result);
                }
            }

            if ($this->menuLogic->add($data)){
                $result['status'] = true;
                $result['msg'] = '新增成功！';
                return response()->json($result);
            }

            return response()->json($result);
        } else {
            if ($id < 1 || empty($id)) $id = 0;
            $subArr = $this->menuLogic->sub2arr();
            return view('menu.add', ['sub' => $subArr, 'pid'=>$id]);
        }
    }

    public function edit(Request $request, $id = 0)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'修改失败！'];
            $id = $request->post('id',0);
            $data['pid'] = $request->post('pid',0);
            $data['menu_type'] = $request->post('menu_type',1);
            $data['menu_name']= $request->post('menu_name','');
            $data['menu_url']= $request->post('menu_url','#');
            $data['menu_sort'] = $request->post('menu_sort',0);
            $data['menu_icon']= $request->post('menu_icon','');
            $data['menu_status'] = $request->post('menu_status',1);

            if(($id < 1) || !($this->menuLogic->has((int)$id))){
                $result['msg'] = '请勿非法提交！';
                return response()->json($result);
            }

            if($id <= 7){
                $result['msg'] = '本菜单禁止修改！';
                return response()->json($result);
            }

            if($id == $data['pid']){
                $result['msg'] = '不能选择自己作为父级！';
                return response()->json($result);
            }

            if(!$this->menuLogic->checkName($data['menu_name'],(int)$data['pid'],(int)$id)){
                $result['msg'] = '菜单名称已经存在！';
                return response()->json($result);
            }

            if ($data['menu_url'] != '#' && !empty($data['menu_url'])){
                if(!$this->menuLogic->checkUrl($data['menu_url'],(int)$id)){
                    $result['msg'] = '链接地址已经存在！';
                    return response()->json($result);
                }
            }

            $validator = Validator::make($request->all(), $this->menuRequest->rules(), $this->menuRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if ($this->menuLogic->update((int)$id, $data)){
                $result['status'] = true;
                $result['msg'] = '修改成功！';
                return response()->json($result);
            }

            return response()->json($result);
        } else {
            if ($id < 1 || empty($id)) abort(404);
            $data = $this->menuLogic->find((int)$id);
            if (empty($data)) abort(404);
            $subArr = $this->menuLogic->sub2arr();
            return view('menu.edit', ['sub' => $subArr, 'data'=>$data]);
        }
    }

    public function status($id = 0)
    {
        $data = ['status' => false, 'msg' => '状态设置失败！'];
        if ($id > 0){
            if ($id <= 7){
                $data['msg'] = '本菜单禁止设置状态！';
                return response()->json($data);
            }
            if ($this->menuLogic->status((int)$id)){
                $data['status'] = true;
                $data['msg'] = '状态设置成功！';
                return response()->json($data);
            }
        }
        return response()->json($data);
    }

    public function delete(int $id = 0)
    {
        $result = ['status'=>false, 'msg'=>'删除失败！'];
        if(($id < 1) || !($this->menuLogic->has((int)$id))){
            $result['msg'] = '请勿非法提交！';
            return response()->json($result);
        }

        if($id <= 7){
            $result['msg'] = '本菜单禁止删除！';
            return response()->json($result);
        }

        $subCount =$this->menuLogic->subCount((int)$id);
        if ($subCount > 0){
            $result['msg'] = '本菜单下有子菜单，不能删除！';
            return response()->json($result);
        }

//        $roleCount =$this->menuLogic->countRoleByMenuId((int)$id);
//        if ($roleCount > 0){
//            $result['msg'] = '本菜单已经关联角色权限，不能删除！';
//            return response()->json($result);
//        }

        if ($this->menuLogic->delete($id)){
            $result['status'] = true;
            $result['msg'] = '删除成功！';
            return response()->json($result);
        }

        return response()->json($result);
    }
}
